//
//  LBBasicMsgInputView.m
//  app
//
//  Created by 杨庆人 on 16/12/23.
//  Copyright © 2016年 广东联结电子商务有限公司. All rights reserved.
//

#import "LBBasicMsgInputView.h"
#import <Masonry/Masonry.h>
#import <LBCategory/LBCategory.h>

@interface LBBasicMsgInputView ()<UITextFieldDelegate>


@property (nonatomic,strong)UILabel * bottomLabel;

@property (nonatomic,copy)NSString * title;
@property (nonatomic,copy)NSString * placeholder;
@property (nonatomic,assign)NSInteger maxNum;
@property (nonatomic,copy)NSString * bottomText;
@property (nonatomic,copy)NSString * chatStr;
@property (nonatomic,assign)UIKeyboardType keyboardType;
@property (nonatomic,copy)void(^mCkickAtion)();

@end
@implementation LBBasicMsgInputView

- (instancetype)initWithTitle:(NSString *)title placeholder:(NSString *)placeholder maxWordNum:(NSInteger)maxNum bottomText:(NSString *)bottomText chatStr:(NSString *)chatStr keyboardType:(UIKeyboardType)keyboardType {
    if (self = [super init]) {
        _title = title;
        _placeholder = placeholder;
        _maxNum = maxNum;
        _bottomText = bottomText;
        _chatStr = chatStr;
        _keyboardType = keyboardType;
        
        [self setBasicMsgInputView];
        
        if (_chatStr.length) {
            NSString * string = [NSString stringWithFormat:@"%@%@",_title,_chatStr];
            NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithString:string];
            [attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:74/255.0 green:144/255.0 blue:226/255.0 alpha:1] range:[string rangeOfString:_chatStr]];
            self.titleLabel.attributedText = attrStr;
        }
        else {
            self.titleLabel.text = _title;
        }
    }
    return self;
}

- (instancetype)init {
    if (self = [super init]) {
        [self setBasicMsgInputView];
    }
    return self;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont systemFontOfSize:12];
        _titleLabel.textColor = [UIColor colorWithR:134 G:134 B:134 A:1];
    }
    return _titleLabel;
}
- (UILabel *)bottomLabel {
    if (!_bottomLabel) {
        _bottomLabel = [[UILabel alloc] init];
        _bottomLabel.font = [UIFont systemFontOfSize:12];
        _bottomLabel.textColor = [UIColor colorWithRed:74/255.0 green:144/255.0 blue:226/255.0 alpha:1];
        _bottomLabel.text = _bottomText;
    }
    return _bottomLabel;
}
- (LBUnderlineTextField *)textField {
    if (!_textField) {
        _textField = [[LBUnderlineTextField alloc] init];
        _textField.font = [UIFont systemFontOfSize:14];
        _textField.textColor = [UIColor blackColor];
        _textField.placeholder = _placeholder;
        _textField.keyboardType = _keyboardType;
        _textField.maxTextLength = _maxNum;
    }
    return _textField;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(self);
        make.height.mas_equalTo(17);
    }];
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self);
        make.height.mas_equalTo(25);
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(8);
    }];
    [self.bottomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self);
        make.height.mas_equalTo(17);
        make.top.mas_equalTo(self.textField.mas_bottom).offset(5);
    }];
}

- (void)setTitleSuffixWithImage:(UIImage *)image mCGRect:(CGRect)mCGRect mClickAtion:(void(^)())mClickAtion {
    
    UIButton * oldButton = [self viewWithTag:1001];
    
    [oldButton removeFromSuperview];

    self.mCkickAtion = mClickAtion;
    
    UIButton * oneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [oneButton setImage:image forState:UIControlStateNormal];
    
    oneButton.tag = 1001;
    
    [self addSubview:oneButton];
    //original 12
    CGSize newSize = CGSizeMake(20, 20);

    [oneButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLabel.mas_top).offset(mCGRect.origin.y - 4);
        make.left.mas_equalTo(self.titleLabel.mas_right).offset(mCGRect.origin.x - 4);
        make.size.mas_equalTo(newSize);
    }];
    
    [oneButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
//
//    [imageView addGestureRecognizer:tap];

    [oneButton setImageEdgeInsets:UIEdgeInsetsMake(4, 4, 4, 4)];
    
}

- (void)buttonClick:(UIButton *)senter
{
    !self.mCkickAtion ? :self.mCkickAtion();
}

//- (void)tapClick:(UITapGestureRecognizer *)tap{
//
//    !self.mCkickAtion ? :self.mCkickAtion();
//}

- (void)setBasicMsgInputView {
    [self addSubview:self.titleLabel];
    [self addSubview:self.textField];
    [self addSubview:self.bottomLabel];
}

- (void)setTitle:(NSString *)title placeholder:(NSString *)placeholder maxWordNum:(NSInteger)maxNum bottomText:(NSString *)bottomText chatStr:(NSString *)chatStr keyboardType:(UIKeyboardType)keyboardType {
    
    if (chatStr.length) {
        NSString * string = [NSString stringWithFormat:@"%@%@",title,chatStr];
        NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithString:string];
        [attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:74/255.0 green:144/255.0 blue:226/255.0 alpha:1] range:[string rangeOfString:chatStr]];
        self.titleLabel.attributedText = attrStr;
    }
    else {
        self.titleLabel.text = title;
    }
    
    self.textField.placeholder = placeholder;
    self.textField.keyboardType = keyboardType;
    self.textField.maxTextLength = maxNum;
    
    self.bottomLabel.text = bottomText;
}


+ (CGFloat)heightForView
{
    return 75;
}

@end




@interface LBUnderlineTextField ()

@property (nonatomic,strong)UIImageView * underline;

@end

@implementation LBUnderlineTextField

- (instancetype)init {
    if (self = [super init]) {
        [self setUnderline];
        [self addNotification];
    }
    return self;
}

- (void)dealloc
{
    [self removeNotification];
}

- (void)addNotification
{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFieldChange) name:UITextFieldTextDidBeginEditingNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFieldChange) name:UITextFieldTextDidEndEditingNotification object:nil];
}

- (void)removeNotification
{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UITextFieldTextDidBeginEditingNotification object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UITextFieldTextDidEndEditingNotification object:nil];
}

- (void)textFieldChange
{
    BOOL isFirst = [self isFirstResponder];
    if (isFirst) {
        [self setBottomLineColorBy:[UIColor colorWithRed:74/255.0 green:144/255.0 blue:226/255.0 alpha:1]];
    } else {
       [self setBottomLineColorBy:[UIColor colorWithR:218 G:218 B:218 A:1]];
    }
    
}

- (void)setBottomLineColorBy:(UIColor *)theColor
{
    if (theColor) {
        self.underline.backgroundColor = theColor;
    }
}

- (UIImageView *)underline{
    if (!_underline) {
        _underline = [[UIImageView alloc] init];
        _underline.backgroundColor = [UIColor colorWithR:218 G:218 B:218 A:1];
    }
    return _underline;
}

- (void)layoutSubviews {
    [super layoutSubviews];

}
- (void)setUnderline {
    [self addSubview:self.underline];

    [self.underline mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(self);
        make.height.mas_equalTo(1);
    }];

}

@end



